import seal from '../../lib/request/seal'
import { RootState } from '../index'
import { Commit } from 'vuex'
import { IResponse } from '../../lib/service'
import { AppliedForMeState } from './appliedForMe'
import project from '@/lib/request/project'

export interface MyManagingSealState extends AppliedForMeState {
  organizeUuid: string
}

const initState: MyManagingSealState = {
  sealList: [],
  total: 0,
  detailVisible: false,
  loading: false,
  sealData: null,
  revokeVisible: false,
  recoverVisible: false,
  lossVisible: false,
  organizeUuid: '',
}

const getters = {}

const actions = {
  async getList(
    context: {
      commit: Commit
      state: MyManagingSealState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    if (payload.projectId) {
      // 查询已授权应用印章
      context.commit('setStateData', { loading: true })
      const data: IResponse = await project
        .queryManageSeal(payload)
        .catch((e: any) => {
          context.commit('setStateData', {
            loading: false,
            sealList: [],
            total: 0,
          })
        })
      context.commit('setStateData', { loading: false })
      if (data && data.data) {
        context.commit('setStateData', {
          sealList: data.data.list || [],
          total: data.data.total,
        })
      }
    } else {
      context.commit('setStateData', { loading: true })
      const data: IResponse = await seal
        .queryMySeals(payload)
        .catch((e: any) => {
          context.commit('setStateData', {
            loading: false,
            sealList: [],
            total: 0,
          })
        })
      context.commit('setStateData', { loading: false })
      if (data && data.data) {
        context.commit('setStateData', {
          sealList: data.data.list || [],
          total: data.data.total,
        })
      }
    }
  },
  async sealDetail(
    context: {
      commit: Commit
      state: MyManagingSealState
      rootState: RootState
      getters: any
    },
    payload: string
  ) {
    const data: IResponse = await seal.sealDetail(payload)

    return data.data
  },
}

const mutations = {
  setStateData(state: MyManagingSealState, data: any) {
    // eslint-disable-next-line no-param-reassign
    state = Object.assign(state, data)
  },
}

export default {
  namespaced: true,
  state: initState,
  getters,
  actions,
  mutations,
}
